<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "auth_shop".
 *
 * @property int $id
 * @property int $uid 所属用户ID
 * @property int $auth_app_id 平台ID
 * @property string|null $auth_app_shop_id
 * @property string|null $name
 * @property string|null $note_name 备注名称
 * @property string|null $avatar 头像
 * @property string $token
 * @property int $token_expire_time token失效时间
 * @property string $refresh_token refresh_token
 * @property int $refresh_token_expire_time refresh_token过期时间
 * @property int $ctime
 * @property int $state 1正常/2取消授权/3删除
 */
class AuthShop extends \yii\db\ActiveRecord
{
    //显示头像
    public function showAvatar(){
        return Html::img($this->avatar,['width'=>60]);
    }
    //显示名称，有备注名显示备注名
    public function showName(){
        return $this->note_name?:$this->nickname;
    }
    //刷新即将过期的token
    static public function refreshTokens($interval = 60){
        $start = time() -10;
        $end = time() + 10 + $interval;
        $objs = self::find()->andWhere("token_expire_time>".$start)->andWhere("token_expire_time<".$end)->andWhere(['state'=>self::STATE_GOOD])->all();
        foreach($objs as $obj){
            $obj->refreshToken();
        }
    }
    //刷新即将过期的refresh_token
    static public function refreshRefreshTokens($interval = 60){
        $start = time() -10;
        $end = time() + 10 + $interval;
        $query = self::find()->andWhere("refresh_token_expire_time>".$start)->andWhere("refresh_token_expire_time<".$end)->andWhere(['state'=>self::STATE_GOOD]);
//        print_r($query->createCommand()->getRawSql());exit;
        $objs = $query->all();
        foreach($objs as $obj){
            $obj->refreshRefreshToken();
        }
    }
    //刷新token
    public function refreshToken(){
        $this->authApp->getAppObj()->refreshToken($this);
    }
    //刷新refresh_token
    public function refreshRefreshToken(){
        $this->authApp->getAppObj()->refreshRefreshToken($this);
    }
    //关联应用
    public function getAuthApp(){
        return $this->hasOne(AuthApp::className(),['id'=>'auth_app_id']);
    }
    //关联应用
    public function getUser(){
        return $this->hasOne(WxUser::className(),['id'=>'uid']);
    }
    //重载删除方法
    public function delete(){
        $this->state = self::STATE_DEL;
        $this->save();
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'auth_shop';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'auth_app_id', 'token', 'token_expire_time', 'refresh_token', 'refresh_token_expire_time', 'ctime'], 'required'],
            [['uid', 'auth_app_id', 'token_expire_time', 'refresh_token_expire_time', 'ctime', 'state'], 'integer'],
            [['auth_app_shop_id', 'name', 'note_name', 'avatar', 'token'], 'string', 'max' => 255],
            [['refresh_token'], 'string', 'max' => 2048],
            [['auth_app_shop_id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'auth_app_id' => '平台',
            'auth_app_shop_id' => '平台店铺ID',
            'name' => '店铺名称',
            'note_name' => '备注名称',
            'avatar' => 'Avatar',
            'token' => 'Token',
            'token_expire_time' => 'Token过期时间',
            'refresh_token' => '刷新Token',
            'refresh_token_expire_time' => '刷新Token过期时间',
            'ctime' => '创建时间',
            'state' => '状态',
        ];
    }
}
